package com.wsx.springai.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wsx.springai.controller.NewsController;
import com.wsx.springai.entity.NewsQueryParams;
import com.wsx.springai.service.PsNewsService;
import com.wsx.springai.utils.HttpUtil;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.List;

/**
 * @description:
 * @create: 2025-07-16-10:28
 * @athor: wsx
 */
@Service
public class PsNewsServiceImpl implements PsNewsService {

    private static final String COOKIES = "JSESSIONID=CADD55E4BBAF23E2877F2BA719C317BA; SHAREJSESSIONID=287688e9-b788-42a3-bb5e-91ad810efe5a; u_ukey=A10702B8689642C6BE607730E11E6E4A; u_uver=1.0.0; u_dpass=ZpmMBShNZCzV15a9Rskgeqc7pjCJYG17dRruLNC8lm9sPp%2FjFm1dNxCL4hWHcjKjHi80LrSsTFH9a%2B6rtRvqGg%3D%3D; u_did=2D09C963BBC94656B0DC0927974E6011; u_ttype=WEB; ttype=WEB; user=MDpkYl90ZXN0XzAwOTo6Tm9uZTo1MDA6NDQwNDY1MDQ4OjcsMTExMTExMTExMTEsNDA7NDQsMTEsNDA7NiwxLDQwOzUsMSw0MDsxLDEwMSw0MDsyLDEsNDA7MywxLDQwOzUsMSw0MDs4LDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAxLDQwOzEwMiwxLDQwOjo6OjQzMDQ2NTA0ODoxNzUyNjMyOTYxOjo6MTUxNDQ0NzA0MDo2MDQ4MDA6MDoxZTQ2YzU0MDRkMTUxNzExY2ViMDI1MjU2ODIwM2IyMmE6ZGVmYXVsdF81OjA%3D; userid=430465048; u_name=db_test_009; escapename=db_test_009; ticket=e8c62628b63f8565a5c4323dcaa0fb78; user_status=0; utk=8101f6f52810ae34a9bd346f7892a44e; orgid=145334; v=AzClsS_uLEToOPBA1PySMGA-AfWHeRRzNlxoxyqA_dpe_t4r0onkU4ZtOEx5";


    //@Tool(description = "获取新闻列表")
    @Override
    public List<JSONObject> getNewsList(String stockName,String date) {
        String url = "https://board.10jqka.com.cn/psmonitor/news/getCompanyNews";
        NewsQueryParams params = getNewsQueryParams(date);
        System.out.println(JSON.toJSONString(params));
        String res = HttpUtil.postJson(url, JSON.toJSONString(params), Collections.singletonMap("Cookie", COOKIES), null);
        System.out.println(res);
        JSONObject jsonObject = JSONObject.parseObject(res);
        return jsonObject.getJSONArray("rows").toJavaList(JSONObject.class);
    }

    private static NewsQueryParams getNewsQueryParams(String date) {
        NewsQueryParams params = new NewsQueryParams();
        params.setPageNum(1);
        params.setNumPerPage(10);
        params.setOrgIds("T000025753");
        params.setThsId("430465048");
        LocalDate startDate = LocalDate.of(Integer.parseInt(date.split("-")[0]),Integer.parseInt(date.split("-")[1]),1);
        LocalDate endDate = LocalDate.of(Integer.parseInt(date.split("-")[0]),Integer.parseInt(date.split("-")[1]),1).plusMonths(1);
        params.setStartDate(startDate.atStartOfDay().toEpochSecond(ZoneOffset.ofHours(8)));
        params.setEndDate(endDate.atStartOfDay().toEpochSecond(ZoneOffset.ofHours(8))-1);
        params.setIsMerge(true);
        return params;
    }
}
